<template>
    <div class="order-main all-main my-all">
        <section class="order-main-header">
             <mt-header title="共识生态">
            <mt-button  icon="back" slot="left" @click="handleClose()"></mt-button>
               <mt-button  slot="right" @click="handleNotice()"><img src="../../assets/action/not.png"></mt-button>
            </mt-header>
        </section>
        <div class=" load-content">
            <div class=" load-main" >
                <div class="container make load-conten">
                    <div class="page-tab-container load-main" >
                        <div class="cash">
                            <div class="list">
                                <span class="head">{{symbol.fShortName}}</span>
                                <div class="sure">
                                    <div class="flex-center flex-column">
                                        <span>买入均价(USDT)</span>
                                        <span class="color-be">{{userReport.buyPrice}}&nbsp;</span>
                                    </div>
                                    <div class="flex-center flex-column" style="">
                                        <span>数量&nbsp;({{symbol.fShortName}})</span>
                                        <span class="color-be">{{userReport.cashAmount}}</span>
                                    </div>
                                    <div class="flex-center flex-column">
                                        <span>共识金额(USDT)</span>
                                        <span class="color-be">{{userReport.reportPrice}}&nbsp;</span>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="recold">共识记录</div>
                        <mt-tab-container class="page-tabbar-tab-container content">
                            <mt-tab-container-item  >
                                <div >
                                    <div class="order-table" v-for="(orderData, i) in entrustList" v-bind:key="i" >
                                        <div class="order-header">
                                            <div class="flex-between ">
                                                <span v-if="orderData.fentrustType==1" class="order-sell">卖出{{symbolList.fShortName}}</span>
                                                <span v-if="orderData.fentrustType==0" class="order-buy">买入{{symbolList.fShortName}}</span>
                                                <p style="font-size:20px;">{{ timestampToTime(orderData.fcreateTime)}}</p>
                                                <button class="upper submit start"   id="submit" v-if="orderData.fstatus==1">未成交</button>
                                                <button class="upper submit start"   id="submit" v-if="orderData.fstatus==2">部分成交</button>
                                                <button class="upper submit start"   id="submit" v-if="orderData.fstatus==3">已成交</button>
                                                <button class="upper submit end"   id="submit" v-if="orderData.fstatus==4">撤销 </button>
                                            </div>
                                        </div>
                                        <router-link :to="{path:'/trade/details',query:{id:orderData.fid,symbol:symbol.fid}}">
                                        <div class="order-content flex-between">
                                            <div class="flex-center">
                                            <div class="all-amount">
                                                <span>委托价({{symbolList.group}})</span>
                                                <span class="color">{{filterDecimals(orderData.fprize)}}</span>
                                            </div>
                                            <div class="all-amount">
                                                <span>成交量({{symbolList.fShortName}})</span>
                                                <span class="color">{{filterDecimals(orderData.fcount-orderData.fleftCount)}}</span>
                                            </div>
                                            <div class="all-amount">
                                                <span>成交总额({{symbolList.group}})</span>
                                                <span class="color">{{orderData.fsuccessAmount}}</span>
                                            </div>
                                            </div>
                                        </div>
                                        <div class="order-content flex-between">
                                            <div class="flex-center">
                                            <div class="all-amount">
                                                <span>成交均价({{symbolList.group}})</span>
                                                <span class="color">{{filterDecimals(orderData.dealPrice)}}</span>
                                            </div>
                                            <div class="all-amount">
                                                <span>委托量({{symbolList.fShortName}})</span>
                                                <span class="color">{{orderData.fcount}}</span>
                                            </div>
                                                <div class="all-amount">
                                                <span>订单类型</span>
                                                <span class="color" v-if="orderData.tradeType==0">普通</span>
                                                    <span class="color" v-if="orderData.tradeType==1"> 共识</span>
                                            </div>
                                            </div>
                                        </div>
                                        </router-link>
                                    </div>
                                </div>
                            </mt-tab-container-item>
                        </mt-tab-container>
                    </div>
                    <a   class="total-btn" @click="security()">{{userDisabledTitle}}</a>
                    <section class="el-toast" v-if="cationModal">
                            <div class="el-modal">
                                <div class="modal-top">
                                    <div class="modal-top-error" @click="handleNoticeCancel()"><img src="../../assets/action/error.png"></div>
                                    <div class="modal-header">
                                            <span> 共识生态</span>
                                        </div>
                                        <div class="modal-header-content">
                                            <span> 1用户参与共识买卖后，可参与亏损补贴以及邀请奖励活动。</span>
                                            <span> 2用户最多共识买入1000USDT,当用户共识买卖低于100USDT时，无法参与活动。</span>
                                        </div>
                                </div>
                                    <div class="modal-bottom">
                                    <div class="modal-bottom-content">
                                            <span>3卖出价及卖出均价必须在买入均价*90%以下或买入均价*10%-买入均价110%之间，否则会被禁止共识。如被禁止共识请联系客服申请开通。</span>
                                            <span>4当共识卖出后，必须将共识买入持币全部卖出后，才能进行再次共识。</span>
                                            <span>5共识买入后，共识全部卖出算一个周期，一日只允许共识买卖一个周期。共识买入金额未达到1000USDT，可继续共识买入。</span>
                                        </div>
                                </div>
                            </div>
                    </section>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
import { Toast } from 'mint-ui';
import { MessageBox } from 'mint-ui';
   import {mapMutations, mapActions} from 'vuex';
export default {
    name:'myOrder',
    data(){
         return {
             entrustList:[],
             historyOrdesList:[],
             fid:'',
             entrustListLog:[],
             symbol:'',
             symbolList:'',
             report:'',
             userReport:{buyPrice:'0',cashAmount:'0',reportPrice:'0'},
             userDisabled:'',
                userDisabledTitle:'',
                cationModal:false
         }
    },
    mounted() {
        this.getOtcList();
    },
    methods: {
          ...mapMutations([
                'SET_REPORT','SET_HOME_PAGE'
            ]),
              handleNoticeCancel(){
            this.cationModal=false;
        },
        handleNotice(){
            this.cationModal=true;
        },
        filterDecimals(res){
              if(!res){
                  return 0
              }
            return res.toFixed(4);
        },
        security(){
            if(this.userDisabled==2){
            Toast("由于共识卖出价格高于买入均价10%，共识生态被禁用。请联系客服，线下申请重新开通。");
            }else if(this.userDisabled==1){
                 this.SET_REPORT({type:1});
                this.SET_HOME_PAGE({homePage:3});
                 this.$router.push({
          path: `/home/${'?symbol='+this.symbol.fid}`
        });
            }
        },
         cancelOrder(order){
            this.$http.post('/v2/market/cancelEntrust', { id: order })
                .then(this.refreshUser(this.symbol))
        },
         getOtcList(){this.$http.post('/v2/market/allcoins',).then((res)=>{
                let tickers = res.data;
                 let coinGroup = {}
                    let param = location.hash.match(new RegExp(".*" + 'symbol' + "=([^\&]*)(\&?)", "i"));
                    let symbol = param && !isNaN(param[1]) ? param[1] : 0;
                    let current = null;
                    for (let i = 0; i < tickers.length; i++) {
                        let ticker = tickers[i]
                        if(ticker.report){
                            current = ticker
                        }
                    }
                    let newGroup = this.objKeySort(coinGroup);
                    if (!current) {
                        for (let group in newGroup) {
                            current = newGroup[group][0]
                            break;
                        }
                    }
                    this.symbolList=current;
                   this.symbol=current;
            this.refreshUser(current.fid);
            this.userReportsData(current.fid);
             })
    },
                objKeySort(obj) {
            var newkey = Object.keys(obj).sort();
            var newObj = {};
            for (var i = 0; i < newkey.length; i++) {
                newObj[newkey[i]] = obj[newkey[i]];
            }
            return newObj;
        },
        userReportsData(symbol){
               let self=this;
              let param={
                  coinId:symbol,
              }
            this.$http.post('/v2/market/userReportsData',param)
                .then(function(res) {
                     self.userReport=res.data.data;
                })
        },
        refreshUser(symbol){
              let self=this;
              let param={
                  coinId:symbol,
                  stauts:'0',
                  page:1,
                  pageSize:20
              }
            this.$http.post('/v2/market/marketReportRefresh',param)
                .then(function(res) {
                    self.entrustList=res.data.data.filter((res)=>{
                        return res.tradeType==1
                    });
                    self.userIsDisabled();
                })
        },
        userIsDisabled(){
            let self=this;
            this.$http.post('/v2/market/userIsDisabled',{symbol:this.symbol.fid})
                .then(function(res) {
                   self.userDisabled=res.data.data.status;
                    if(self.userDisabled==2){
                         self.userDisabledTitle='查看禁止共识原因';
            }else if(self.userDisabled==1){
                   self.userDisabledTitle='去共识买卖';
            }
                })
        },
        tabOrder(data){
            this.orderTabs=data
        },
            handleClose(){
                this.$router.go(-1);
            },
                  timestampToTime (cjsj) {
        var date = new Date(cjsj) //时间戳为10位需*1000，时间戳为13位的话不需乘1000
        var Y = date.getFullYear() + '-'
        var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-'
        var D = date.getDate() + ' '
        var h = date.getHours() + ':'
        var m = date.getMinutes() + ':'
        var s = date.getSeconds()
        return Y+M+D+h+m+s
        console.log(timestampToTime (1533293827000))
    },
    },
}
</script>
<style lang='less' scoped>
.my-all{
.order-main .sell{
width: 50%;
height: 100px;
line-height: 100px;
text-align: center;
}
.order-main-header{
    display: flex;
    flex-direction: row;
    height: 100px;
}
.make .recold{
    height: 80px;
    line-height: 80px;
    font-size:28px;
      color: rgba(224, 224, 249, 1);
}
.color{
    color: rgba(224, 224, 249, 1);
}
.make .order-sell{
    color: #F36D7D;
    font-size:32px;
}
.order-buy {
    color: #51D6AD;
    font-size:32px;
}
.all-amount{
     display: flex;
    flex-direction: column;
    height: 90px;
    text-align: center;
}
.order-content .all-amount:nth-child(1){
    width: 30%;
}
.order-content .all-amount:nth-child(2){
    width: 40%;
}
.order-content .all-amount:nth-child(3){
    width: 30%;
}
.all-amount span:nth-child(1){
    height: 60px;
    line-height: 60px;
}

.make .page-tab-container{
    height: 100%;
        padding-left: 30px;
    padding-right: 30px;
}
.data-img{
        min-height: 850px;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column
}
.data-img img{
     width: 120px;
    height: 120px;
}
.order-content .left{
    color: #E0E0F9
}
.order-header{
    height: 80px;
    line-height: 80px;
    border-bottom:1px solid #161736;
    color: #ADADD8;
}
.all-main.order-header p{
  margin-left: -60px
}
.rect{
    height:10px;
    background:rgba(244,244,244,1);
}
.order-content{
    padding-top: 20px;
    color: #ADADD8;
    font-size: 24px;
}
.order-main .left p{
    height: 40px;
    font-size: 24px;
}
.nav{
    display: flex;
    flex-direction: row;
    border-bottom: 1px solid rgba(34,34,64,1);
}
.black{
    color: #E0E0F9;
    font-weight:600;
    font-size:32px;
}
.flex-center{
    display: flex;
    width: 100%;
   align-self: center;
}
.all-main .flex-center{
width: 100%;
}
.flex-between{
      display: flex;
        justify-content: space-between;
    }
    .end{
        background: #8c9fad
    }
    .start{
          background:#5298FE;
          border-radius:6px;
    }
    .submit {
    width: 120px;
    height: 48px;
    font-size: 28px;
    line-height: 48px;
    font-weight: 700;
    color: #fff;
    border-radius:5px;
}
.order-header>div{
    align-items: center;
}
.order-table{
    padding: 20px 30px 20px 30px;
    background: #222240;
    position: relative;
    margin-bottom: 20px;
}
.tabs .img{
    width: 40px;
    height: 40px;
    position: relative;
    right: 20px;
    top:30px;
}
.order-main .buy{
width: 50%;
height: 100px;
line-height: 100px;
text-align: center;
}
.order-main .buy span{
text-align: center;
width: 50%;
line-height: 100px;
height: 90%;
}
.flex-content{
      display: -ms-flexbox;
    display: flex;
    justify-content: center;
}
 .make  .total-btn {
            display: block;
            width: 90%;
            line-height: 58px;
            font-size: 30px;
            text-align: center;
            margin-bottom: 0;
           border-radius:10px;
            height:86px;
            font-size:32px;
            line-height: 86px;
            color: #FFFFFF;
            background:rgba(82,152,241,1);
            z-index: 10001;
            position: fixed;
            bottom: 30px;
             left: 0px;
    right: 0px;
    margin-left: auto;
    margin-right: auto;
    }
.order-main .sell span{
width: 50%;
height: 90%;
line-height: 100px;
}
.tab-content{
    width: 80%;
    margin: 0 auto;
    font-size: 32px;
    font-weight:500;
}
.order-main .tab{
display: flex;
background:rgba(255,255,255,1);
height: 100px;
border-bottom:1px solid #161736
}
.order-main .active{
color: #3381FF;
}
.mint-header{
    height: 100px;
    font-size: 32px;
    background-color: #161736;
    color: #E0E0F9
}
.data-img p{
color: #fff;
}
.make .cash {
    height: 224px;
    height:224px;
    border-radius:8px;
      background: rgba(34,34,64,1);
}
.make .cash .list {
    position: relative;
    padding: 30px;
   
}
 .make .cash .list .head{
     line-height: 70px;
     height: 70px;
     font-size:36px;
     color: rgba(255, 255, 255, 1);
 }
 .color-be{
     color: rgba(224, 224, 249, 1)
 }
.make .cash .list .sure {
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-orient: horizontal;
    -webkit-box-direction: normal;
    -ms-flex-direction: row;
    flex-direction: row;
    color: #8686C0;
    line-height: 50px;
    font-size: 28px;
}
.flex-column{
    flex-direction: column;
}
.make .content{
    min-height: 800px;
    overflow-y: scroll;
    margin-bottom: 100px;
}
.all-main{
    height: 100%;
}
.end{
    background: rgba(33, 61, 102, 1);
    color: rgba(173, 173, 216, 1)
}
}.el-toast {
    .el-modal{
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100%;
        flex-direction: column;
        margin-top: -25%
    }
    .modal-top{
        width:638px;
    height:300px;
    background:rgba(34,34,64,1);
    border-radius:4px 4px 0px 0px;
    color: #fff;
    display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        .modal-header {
            font-size:36px;
            height: 60px;
            position: relative;
            top:-20px;
        }
        .modal-header-content{
            text-align: left;
            line-height: 40px;
            padding-left: 30px;
        }
    }
    .modal-bottom{
            position: relative;
    top: -2px;
         .modal-bottom-content{
             color: #fff;
             span{
                 line-height: 45px;
             }
              padding-left: 30px;
              text-align: left;
              p{
                  height: 70px;
                  line-height: 60px;
                   font-size:28px;
                 color: rgba(82, 152, 241, 1);
              }
             span :nth-child(1){
                 line-height: 60px;
                height: 60px;
             }
         }
        width:638px;
    height:460px;
    background:#222240;
    border-radius:4px 4px 0px 0px;
    }
    .flex-between{
      display: flex;
        justify-content:start
    }
    height: 100%;
    margin-top: 100px;
    position: fixed;
    background: rgba(22, 23, 54, 1);
    width: 100%;
    text-align: center;
    bottom: 0;
    top: 0%;
    z-index: 10000;
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
    -webkit-transition: -webkit-transform 0.3s ease-out;
    transition: -webkit-transform 0.3s ease-out;
    transition: transform 0.3s ease-out;
    transition: transform 0.3s ease-out, -webkit-transform 0.3s ease-out;
}
.toast-modal{
    background: #161736;
}
.modal-top-error{
    position: relative;
    left: 280px;
    float: right;
    top: -10px
}
  .my-all .load-content{
           position: absolute;
           top:100px;
           bottom: 30px;
           overflow-y: scroll;
           -webkit-overflow-scrolling:touch;
           width: 100%;
       }
    .my-all   .load-main{
           height: 2000px;
       }
</style>